<template>
  <div class="lg-soar-header-layout">
    <t-head-menu
      id="lg-soar-layout-header"
      :class="menuCls"
      :theme="theme"
      expand-type="popup"
      :value="active"
      :style="{ 'background-color': settingStore.headerBgColor }"
    >
      <template #logo>
        <span v-if="showLogo" class="header-logo-container" @click="handleNav('/')">
          <img :src="logo" />
          <span style="font-size: 26px; padding-left: 12px">{{ appTitle }}</span>
        </span>
        <div v-else class="header-operate-left">
          <t-button theme="default" shape="square" variant="text" @click="changeCollapsed">
            <t-icon class="collapsed-icon" name="view-list" />
          </t-button>
          <search :layout="layout" />
        </div>
      </template>
      <template v-if="layout !== 'side'" #default>
        <menu-content class="header-menu" :nav-data="menu" />
      </template>
      <template #operations>
        <div class="operations-container">
          <!-- 搜索框 -->
          <a class="inner-search" title="我要提bug" href="https://gitee.com/lgx1992/lg-soar/issues" target="_blank">
            <t-button theme="default" shape="square" variant="text"><t-icon name="bug" /></t-button>
          </a>
          <!-- 搜索框 -->
          <a class="inner-search" title="点个star" href="https://gitee.com/lgx1992/lg-soar" target="_blank">
            <t-button theme="default" shape="square" variant="text"><t-icon name="star" /></t-button>
          </a>
          <!-- 搜索框 -->
          <span class="inner-search" title="菜单搜索" @click="searchDrawerRef.open()">
            <t-button theme="default" shape="square" variant="text"><t-icon name="search" /></t-button>
          </span>

          <!-- 全局通知 -->
          <notice />
          <t-dropdown :min-column-width="135" trigger="click">
            <template #dropdown>
              <t-dropdown-menu>
                <t-dropdown-item class="operations-dropdown-container-item" @click="handleNav('/my/index')">
                  <t-icon name="user-circle"></t-icon>个人中心
                </t-dropdown-item>
                <t-dropdown-item class="operations-dropdown-container-item" @click="updatePasswordModalRef.open()">
                  <t-icon name="lock-on"></t-icon>修改密码
                </t-dropdown-item>
                <t-dropdown-item
                  v-if="tenantList.length > 1"
                  class="operations-dropdown-container-item"
                  @click="tenantModalRef.open()"
                >
                  <t-icon name="portrait"></t-icon>切换租户
                </t-dropdown-item>
                <t-dropdown-item class="operations-dropdown-container-item" @click="handleLogout">
                  <t-icon name="poweroff"></t-icon>退出登录
                </t-dropdown-item>
              </t-dropdown-menu>
            </template>
            <t-button class="header-user-btn" theme="default" variant="text">
              <template #icon>
                <!--                <t-icon class="header-user-avatar" name="user-circle" />-->
                <!--                <t-avatar :image="user?.avatar" />-->
                <img
                  class="header-user-avatar"
                  :src="getFileAccessAuthUrl(user?.avatar, 's')"
                  onerror="this.src='/avatar.png'"
                />
              </template>
              <div class="header-user-account">{{ user?.nickname }}</div>
              <template #suffix><t-icon name="chevron-down" /></template>
            </t-button>
          </t-dropdown>
          <t-tooltip placement="bottom" content="系统设置">
            <t-button theme="default" shape="square" variant="text" @click="toggleSettingPanel">
              <t-icon name="setting" />
            </t-button>
          </t-tooltip>
        </div>
      </template>
    </t-head-menu>
    <update-password-modal ref="updatePasswordModalRef" />
    <tenant-modal ref="tenantModalRef" :options="tenantList" />
    <search-drawer ref="searchDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import type { PropType } from 'vue';
import { computed, defineAsyncComponent, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useAccountStore, useSettingStore } from '@/store';
import { getFileAccessAuthUrl } from '@/soar';
import { getActive } from '@/router';
import { appTitle } from '@/config/global';
import logo from '@/assets/app/logo.png';
import type { MenuRoute } from '@/types/interface';
import Notice from './Notice.vue';
import MenuContent from './MenuContent.vue';
import { tenants } from '@/api/account';

const UpdatePasswordModal = defineAsyncComponent(() => import('@/components/Account/UpdatePasswordModal.vue'));
const TenantModal = defineAsyncComponent(() => import('@/components/Account/TenantModal.vue'));
const SearchDrawer = defineAsyncComponent(() => import('@/layouts/components/SearchDrawer.vue'));

const props = defineProps({
  theme: {
    type: String,
    default: '',
  },
  layout: {
    type: String,
    default: 'top',
  },
  showLogo: {
    type: Boolean,
    default: true,
  },
  menu: {
    type: Array as PropType<MenuRoute[]>,
    default: () => [],
  },
  isFixed: {
    type: Boolean,
    default: false,
  },
  isCompact: {
    type: Boolean,
    default: false,
  },
  maxLevel: {
    type: Number,
    default: 3,
  },
});

const router = useRouter();
const settingStore = useSettingStore();
const accountStore = useAccountStore();
const updatePasswordModalRef = ref();
const tenantModalRef = ref();
const searchDrawerRef = ref();

const { user } = accountStore;

const toggleSettingPanel = () => {
  settingStore.updateConfig({
    showSettingPanel: true,
  });
};

const active = computed(() => getActive());

const menuCls = computed(() => {
  const { isFixed, layout, isCompact } = props;
  return [
    {
      [`lg-soar-header-menu`]: !isFixed,
      [`lg-soar-header-menu-fixed`]: isFixed,
      [`lg-soar-header-menu-fixed-side`]: layout === 'side' && isFixed,
      [`lg-soar-header-menu-fixed-side-compact`]: layout === 'side' && isFixed && isCompact,
    },
  ];
});

const changeCollapsed = () => {
  settingStore.updateConfig({
    isSidebarCompact: !settingStore.isSidebarCompact,
  });
};

const handleNav = (url) => {
  router.push(url);
};

const handleLogout = () => {
  accountStore.logout();
  router.push({
    path: '/login',
    query: { redirect: encodeURIComponent(router.currentRoute.value.fullPath) },
  });
};

const tenantList = ref([]);
tenants().then((res) => {
  tenantList.value = res;
});
</script>
<style lang="less" scoped>
.lg-soar-header {
  &-menu-fixed {
    position: fixed;
    top: 0;
    z-index: 1001;

    &-side {
      left: 232px;
      right: 0;
      z-index: 10;
      width: auto;
      transition: all 0.3s;
      &-compact {
        left: 64px;
      }
    }
  }

  &-logo-container {
    cursor: pointer;
    display: inline-flex;
  }
}
.header-menu {
  flex: 1 1 1;
  display: inline-flex;

  :deep(.t-menu__item) {
    min-width: unset;
    padding: 0px 16px;
  }
}

.operations-container {
  display: flex;
  align-items: center;
  margin-right: 12px;

  .t-popup__reference {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .t-button {
    margin: 0 8px;
    &.header-user-btn {
      margin: 0;
    }
  }

  .t-icon {
    font-size: 20px;
    &.general {
      margin-right: 16px;
    }
  }
}

.header-operate-left {
  display: flex;
  margin-left: 20px;
  align-items: normal;
  line-height: 0;

  .collapsed-icon {
    font-size: 20px;
  }
}

.header-logo-container {
  width: 184px;
  height: 26px;
  display: flex;
  margin-left: 24px;
  color: var(--td-text-color-primary);

  .t-logo {
    width: 100%;
    height: 100%;
    &:hover {
      cursor: pointer;
    }
  }

  &:hover {
    cursor: pointer;
  }
}

.header-user-account {
  display: inline-flex;
  align-items: center;
  color: var(--td-text-color-primary);
  .t-icon {
    margin-left: 4px;
    font-size: 16px;
  }
}

:deep(.t-head-menu__inner) {
  border-bottom: 1px solid var(--td-border-level-1-color);
}

.t-menu--light {
  .header-user-account {
    color: var(--td-text-color-primary);
  }
}
.t-menu--dark {
  .t-head-menu__inner {
    border-bottom: 1px solid var(--td-gray-color-10);
  }
  .header-user-account {
    color: rgba(255, 255, 255, 0.55);
  }
  .t-button {
    --ripple-color: var(--td-gray-color-10) !important;
    &:hover {
      background: var(--td-gray-color-12) !important;
    }
  }
}

.operations-dropdown-container-item {
  width: 100%;
  display: flex;
  align-items: center;

  .t-icon {
    margin-right: 8px;
  }

  :deep(.t-dropdown__item) {
    .t-dropdown__item__content {
      display: flex;
      justify-content: center;
    }
    .t-dropdown__item__content__text {
      display: flex;
      align-items: center;
      font-size: 14px;
    }
  }

  :deep(.t-dropdown__item) {
    width: 100%;
    margin-bottom: 0px;
  }
  &:last-child {
    :deep(.t-dropdown__item) {
      margin-bottom: 8px;
    }
  }
}
.header-user-avatar {
  width: 30px;
  margin-right: var(--td-comp-margin-s);
  border-radius: 50%;
}
.inner-search {
  cursor: pointer;
  &:hover {
    color: var(--app-color-primary);
  }
}
</style>
